uniform sampler2D mainTexture;
uniform sampler2D blurTexture;
uniform sampler2D raysTexture;

uniform float windowWidth;
uniform float  windowHeight;

uniform float exposure;
uniform float gamma;
uniform float contrast;
uniform float brightness;
uniform float bloomAmount;

const float MIDDLE_GREY = 0.72;
const float FUDGE = 0.001;
const float L_WHITE = 1.5;

varying vec2 texCoord;

/** Tone mapping function 
  @note Only affects rgb, not a
  @param inColour The HDR colour
  @param lum The scene lumninence 
  @returns Tone mapped colour
 */
vec3 toneMap(in vec3 inColour, in float lum)
{
	// From Reinhard et al
	// Initial luminence scaling (equation 2)
	inColour*= MIDDLE_GREY / (FUDGE + lum);
	// Control white out (equation 4 nom)
	inColour*= (1.0 + inColour/ L_WHITE);
	// Finalmapping (equation 4 denom)
	inColour/= (1.0 + inColour);
	return inColour;
}

void main()
{
	float expo = exp(1.0-exposure);
	float HDRgam =exp(-gamma); 

	vec3 color = vec3(0.0);
	vec3 sample;
	vec4 origsample;

	origsample = texture2D(mainTexture,texCoord);

	sample = pow(origsample.rgb, vec3(2.2));		// gamma
	color=sample;

	if(origsample.a > 0.1)
	{
		sample = texture2D(blurTexture,texCoord).rgb;
		sample = pow(sample, vec3(2.2));		// gamma
		color +=sample*bloomAmount;
	}
	color =toneMap(color, expo);
	color *= vec3(1.0+contrast);
	color= pow(color, vec3(1.0/(2.2+gamma)));		// gamma
//	color+=10.0* pow(texture2D(raysTexture,texCoord).rgb,vec3(3.0));
	gl_FragColor.rgb = color;
	gl_FragColor.a = 1.0;
}


